package org.jeecg.modules.gather.component;

import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

@Component
public class DetailsPageSpider {
    private final DetailsPagePageProcessor detailsPagePageProcessor;
    private Spider spider;

    @Autowired
    public DetailsPageSpider(DetailsPagePageProcessor detailsPagePageProcessor) {
        this.detailsPagePageProcessor = detailsPagePageProcessor;
        this.spider = Spider.create(detailsPagePageProcessor)
                .thread(6);
    }

    public String startDetailsPageTask(String url) {
        if (!StringUtils.isEmpty(url)) {
            spider = this.spider.addUrl(url);
            spider.run();
            return "success";
        }
        return "error";
    }

    public String stopDetailsPageTask() {
        if (spider != null) {
            spider.stop();
            return "success";
        }
        return "error";
    }
}
